package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.net.okhttp.INetCallBack;
import com.example.net.okhttp.OkHttpUtils;

import java.util.HashMap;

public class HttpActivity extends AppCompatActivity {
    private Button mBtnGet;
    private Button mBtnPost;
    private Button mBtnPostMultipart;
    private Button mBtnPostJson;

    private TextView mTvContent;
    private final String getUrl = "http://www.imooc.com/api/okhttp/getmethod?username=hyman";
    private final String postUrl = "http://www.imooc.com/api/okhttp/postmethod";
    private final String postJson = "http://www.imooc.com/api/okhttp/postjson";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
        initViews();
        initEvents();
    }

    private void initViews() {
        mBtnGet = findViewById(R.id.get_button);
        mBtnPost = findViewById(R.id.post_button);
        mBtnPostMultipart = findViewById(R.id.post_multipart);
        mBtnPostJson = findViewById(R.id.post_json);
        mTvContent = findViewById(R.id.tv_content);
    }

    private void initEvents() {
        mBtnGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("author_header", "hyman");
                //发起网络请求
                OkHttpUtils.getInstance()
                        .doGet(getUrl, headers, new INetCallBack() {
                            @Override
                            public void onSuccess(String response) {
                                mTvContent.setText(response);
                            }

                            @Override
                            public void onFailed(Throwable e) {
                                Toast.makeText(HttpActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        });
        mBtnPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> params = new HashMap<>();
                params.put("author", "mjt");
                params.put("adb", "mjt post");
                HashMap<String, String> headers = new HashMap<>();
                headers.put("author_header", "hyman");
                OkHttpUtils.getInstance().doPost(postUrl, headers, params, new INetCallBack() {
                    @Override
                    public void onSuccess(String response) {
                        mTvContent.setText(response);

                    }

                    @Override
                    public void onFailed(Throwable e) {
                        Toast.makeText(HttpActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();

                    }
                });
            }
        });
        mBtnPostMultipart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> params = new HashMap<>();
                params.put("author", "mjt");
                params.put("def", "mjt multipart");

                HashMap<String, String> headers = new HashMap<>();
                headers.put("author_header", "hyman");

                OkHttpUtils.getInstance().doPostMultiPart(postUrl, headers, params, new INetCallBack() {
                    @Override
                    public void onSuccess(String response) {
                        mTvContent.setText(response);
                    }

                    @Override
                    public void onFailed(Throwable e) {
                        Toast.makeText(HttpActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();

                    }
                });

            }
        });
        mBtnPostJson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("author_header", "hyman");
                OkHttpUtils.getInstance().doPostJson(postJson, "{\"name\":\"hyman\",\"age\":12}", headers, new INetCallBack() {
                    @Override
                    public void onSuccess(String response) {
                        mTvContent.setText(response);
                    }

                    @Override
                    public void onFailed(Throwable e) {
                        Toast.makeText(HttpActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();

                    }
                });

            }
        });

    }


}